import shutil
from openpyxl.styles import Alignment
from configparser import ConfigParser

from openpyxl import load_workbook
from openpyxl.utils import get_column_letter

from tools_pmc import (fill_blue, today_str1, detect_date, lj_qgmxb, lj_1, lj_config, lj_qgmxb_y
                       )

wb_qgmxb = load_workbook(lj_qgmxb_y)
ws_qgmxb = wb_qgmxb.worksheets[0]

ws_qgmxb.insert_cols(27)
for i in range(2, ws_qgmxb.max_row + 1):
    ws_qgmxb.cell(i, 3).data_type = "int"
    ws_qgmxb.cell(i, 3).number_format = "0"
    ws_qgmxb.cell(i, 3).alignment = Alignment(horizontal='left')
    ws_qgmxb.cell(i, 6).value = detect_date(ws_qgmxb.cell(i, 6).value, 1)
    ws_qgmxb.cell(i, 6).number_format = 'yyyy/mm/dd'
    ws_qgmxb.cell(i, 7).value = detect_date(ws_qgmxb.cell(i, 7).value, 1)
    ws_qgmxb.cell(i, 7).number_format = 'yyyy/mm/dd'
    if ws_qgmxb.cell(i, 1).value == '3104-2024110026':
        ws_qgmxb.cell(i, 9).value = '202411180007'
    if ws_qgmxb.cell(i, 1).value == '3104-2024090027':
        ws_qgmxb.cell(i, 10).value = '销售紧急需求备料（线缆、镜头）'
    if ws_qgmxb.cell(i, 1).value == '3104-2025030023':
        ws_qgmxb.cell(i, 10).value = '销售紧急需求备料（线缆、镜头）'

ws_qgmxb.delete_rows(ws_qgmxb.max_row)

list1 = [18, 8,14, 30, 30, 14, 14, 14, 15, 25, 25, 14]
for i in range(0, len(list1)):
    ws_qgmxb.column_dimensions[get_column_letter(i+1)].width = list1[i]

for i in range(1, ws_qgmxb.max_column+1):
    ws_qgmxb.cell(1, i).fill = fill_blue

del wb_qgmxb['Sheet2']
del wb_qgmxb['Sheet3']
ws_qgmxb.title = "请购明细表" + today_str1
ws_qgmxb.freeze_panes = 'A2'
ws_qgmxb.auto_filter.ref = "A1:M" + str(ws_qgmxb.max_row)

wb_qgmxb.save(lj_qgmxb)

cfg = ConfigParser()
cfg.read(lj_config)
if not cfg.get('set', 'Test mode'):
    shutil.copy(lj_qgmxb, lj_1)
print(today_str1 + ":qgmx  succeed!")
